angular.module('BaseControl')

.directive("pagebutton", ['EntityService', 'BaseEnum', '$stateParams', function(EntityService, BaseEnum, $stateParams) {
    return {
        restrict: "E",
        scope: {
            module: '=?',
            beforeturnpage: '&?',
            afterturnpage: '&?',
        },
        template:
            '<div><div class="LableDiv" style="width:50px;"><div class="Button" ng-click="firstPage()">首页</div></div>' + 
            '<div class="LableDiv" style="width:50px;"><div class="Button" ng-click="prePage()">上页</div></div>' + 
            '<div class="LableDiv" style="width:50px;"><div class="Button" ng-click="nextPage()">下页</div></div>' + 
            '<div class="LableDiv" style="width:50px;"><div class="Button" ng-click="lastPage()">末页</div></div></div>',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            $scope.PageCount = 0;
            $scope.PageNum = -1;
            $scope.FocusedRecordId = null;

            $scope.queryRecordCount = function(completeFun){
                if($scope.module.IsEntityModule){
                    var condition = $scope.module.Condition;
                    if($scope.module.ParentModule != null){
                        if($scope.module.ParentModule.FocusedRecord == null || $scope.module.ParentModule.FocusedRecord.RowStatus == BaseEnum.RowStatus.Insert.Value){
                            $scope.module.Records = [];
                            if (completeFun != undefined && completeFun != null) {
                                completeFun(records);
                            }
                            return;
                        }
                        else{
                            if(condition == null){
                                condition = eval("({" + $scope.module.ParentModule.EntityName + ": '" + $scope.module.ParentModule.FocusedRecord._id + "'})");
                            }
                            else{
                                eval("condition." + $scope.module.ParentModule.EntityName + " = '" + $scope.module.ParentModule.FocusedRecord._id + "'");
                            }
                        }
                    }

                    EntityService.queryRecordCount($scope.module.EntityName, condition,
                        function(count){
                            if(completeFun != undefined && completeFun != null) {
                                completeFun(count);
                            }
                        }
                    );
                }
                else{
                    if(completeFun != undefined && completeFun != null) {
                        completeFun($scope.module.length);
                    }
                }
            };

            $scope.queryRecordCount(function(recordCount) {
                $scope.PageCount = recordCount;
            });

            $scope.firstPage = function(){
                if($scope.beforeturnpage){
                    $scope.beforeturnpage();
                }

                if($scope.PageCount > 0){
                    $scope.assignPage(0);
                }                
            };
            $scope.prePage = function(){
                if($scope.beforeturnpage){
                    $scope.beforeturnpage();
                }

                var pageNum = -1;
                if($scope.module.FocusedRecord != null && 
                    $scope.FocusedRecordId !== $scope.module.FocusedRecord._id && 
                    $scope.module.FocusedRecord.RowStatus != BaseEnum.RowStatus.Insert.Value){
                    $scope.findRecordIndex($scope.module.FocusedRecord, function(rowIndex){
                        $scope.PageCount = rowIndex.Count;
                        $scope.PageNum = rowIndex.Index;

                        if($scope.PageNum > 0){
                            pageNum = $scope.PageNum - 1;
                        }
                        else if($scope.PageCount > 0){
                            pageNum = 0;
                        }

                        if(pageNum > -1){
                            $scope.assignPage(pageNum);
                        }
                    });
                }
                else{                    
                    if($scope.PageNum > 0){
                        pageNum = $scope.PageNum - 1;
                    }
                    else if($scope.PageCount > 0){
                        pageNum = 0;
                    }

                    if(pageNum > -1){
                        $scope.assignPage(pageNum);
                    }
                }
            };
            $scope.nextPage = function(){
                if($scope.beforeturnpage){
                    $scope.beforeturnpage();
                }                

                var pageNum = -1;
                if($scope.module.FocusedRecord != null && 
                    $scope.FocusedRecordId !== $scope.module.FocusedRecord._id &&
                    $scope.module.FocusedRecord.RowStatus != BaseEnum.RowStatus.Insert.Value){
                    $scope.findRecordIndex($scope.module.FocusedRecord, function(rowIndex){
                        $scope.PageCount = rowIndex.Count;
                        $scope.PageNum = rowIndex.Index;

                        if($scope.PageNum > -1) {
                            if($scope.PageCount - 1 > $scope.PageNum){
                                pageNum = $scope.PageNum + 1;
                            }
                            else{
                                pageNum = $scope.PageNum;
                            }
                        }
                        else if($scope.PageCount > 0){
                            pageNum = 0;
                        }

                        if(pageNum > -1){
                            $scope.assignPage(pageNum);
                        }
                    });
                }
                else{ 
                    if($scope.PageNum > -1) {
                        if($scope.PageCount - 1 > $scope.PageNum){
                            pageNum = $scope.PageNum + 1;
                        }
                        else{
                            pageNum = $scope.PageNum;
                        }
                    }
                    else if($scope.PageCount > 0){
                        pageNum = 0;
                    }

                    if(pageNum > -1){
                        $scope.assignPage(pageNum);
                    }
                }
            };
            $scope.lastPage = function(){
                if($scope.beforeturnpage){
                    $scope.beforeturnpage();
                }

                if($scope.module.FocusedRecord.RowStatus != BaseEnum.RowStatus.Insert.Value){
                    if($scope.PageCount > 0 && $scope.PageNum != $scope.PageCount-1){
                        $scope.assignPage($scope.PageCount - 1);
                    }
                }
                else{
                    if($scope.PageCount > 0){
                        $scope.assignPage($scope.PageCount - 1);
                    }
                }
            };

            $scope.assignPage = function(pageNum, completeFun){
                if(pageNum > -1 && pageNum < $scope.PageCount){
                    EntityService.pageQuery($scope.module.EntityName, 
                        $scope.module.Condition, $scope.module.OrderBy, $scope.module.Populate,
                        pageNum, 1,
                        function(records){
                            if(records != null && records != undefined && records.length > 0){
                                records[0].RowStatus = BaseEnum.RowStatus.UnChange.Value;
                                records[0].IsSelected = false;
                                records[0].IsHide = false;
                                $scope.module.Records = records;
                                $scope.module.FocusedRecord = $scope.module.Records[0];
                                $scope.FocusedRecordId = $scope.module.Records[0]._id;

                                $scope.PageNum = pageNum;

                                if($scope.module.AfterOnLoad){
                                    $scope.module.AfterOnLoad(records);
                                }

                                if($scope.afterturnpage){
                                    $scope.afterturnpage(records);
                                }

                                if(completeFun){
                                    completeFun(records);
                                }
                            }
                            else{
                                if(completeFun){
                                    completeFun([]);
                                }
                            }
                            
                        }
                    );
                }
            };

            $scope.findRecordIndex = function(record, completeFun){
                EntityService.findRecordIndex($scope.module.EntityName, record._id, $scope.module.Condition, $scope.module.OrderBy,
                    function(recordIndex){
                        if(completeFun != undefined && completeFun != null) {
                            completeFun(recordIndex);
                        }
                    }
                );
            };
        }],
        link: function (scope, element, attrs) {
            scope.$on("SaveEvent" + scope.module.ID, function(){
                if(scope.module.FocusedRecord != null && scope.module.FocusedRecord != undefined){
                    scope.findRecordIndex(scope.module.FocusedRecord, function(rowIndex){
                        scope.PageCount = rowIndex.Count;
                        scope.PageNum = rowIndex.Index;
                    });
                }
                else{
                    scope.queryRecordCount(function(recordCount) {
                        scope.PageCount = recordCount;
                        scope.PageNum = -1;
                    });
                }
            });

            scope.$on("DeleteEvent" + scope.module.ID, function(event, records){
                var record = records[0];
                if(record.RowStatus == BaseEnum.RowStatus.Insert.Value){
                    scope.PageNum++;
                    if(scope.PageNum > scope.PageCount - 1){
                        scope.PageNum = scope.PageCount - 1;
                    }

                    if(scope.PageNum > -1){
                        scope.assignPage(scope.PageNum);
                    }
                    else{
                        scope.module.add();
                    }
                }
                else{
                    scope.queryRecordCount(function(recordCount) {
                        scope.PageCount = recordCount;
                        scope.PageNum++;
                        if(scope.PageNum > scope.PageCount - 1){
                            scope.PageNum = scope.PageCount - 1;
                        }

                        if(scope.PageNum > -1){
                            scope.assignPage(scope.PageNum);
                        }
                        else{
                            scope.module.add();
                        }
                    });
                }
            });
        }
    }
}]);
